package com.zkingsoft.zkingbase.activities;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.white.hot.libblur.StackBlurManager;
import com.zkingsoft.zkingbase.R;
import com.zkingsoft.zkingbase.base.BaseActivity;
import com.zkingsoft.zkingbase.constant.Constant;
import com.zkingsoft.zkingbase.manager.AliManager;
import com.zkingsoft.zkingbase.manager.WXManager;
import com.zkingsoft.zkingbase.widget.BaseDialog;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/***
 * 微信支付， 需要在配置文件中配置以下信息
 * (1)发起支付的activity
 * <activity
 * android:name=".activities.PayActivity"
 * android:exported="true"
 * android:label="@string/app_name"
 * android:launchMode="singleTop"
 * android:screenOrientation="portrait">
 * <intent-filter>
 * <action android:name="android.intent.action.VIEW" />
 * <category android:name="android.intent.category.DEFAULT" />
 * <data android:scheme="wx0fa3e6f18744566e" />
 * </intent-filter>
 * </activity>
 * <p/>
 * (2)支付回调activity，必须按照此格式
 * <activity
 * android:name=".wxapi.WXPayEntryActivity"
 * android:exported="true"
 * android:launchMode="singleTop" />
 * <p/>
 * (3)可要可不要，记得发起支付前先将app注册到微信
 * <receiver android:name=".wxapi.AppRegister">
 * <intent-filter>
 * <action android:name="com.tencent.mm.plugin.openapi.Intent.ACTION_REFRESH_WXAPP" />
 * </intent-filter>
 * </receiver>
 * <p/>
 * <p/>
 * <p/>
 * 支付宝支付，需要在配置文件中配置以下信息
 * <activity
 * android:name="com.alipay.sdk.app.H5PayActivity"
 * android:configChanges="orientation|keyboardHidden|navigation"
 * android:exported="false"
 * android:screenOrientation="behind" >
 * </activity>
 * <activity
 * android:name="com.alipay.sdk.auth.AuthActivity"
 * android:configChanges="orientation|keyboardHidden|navigation"
 * android:exported="false"
 * android:screenOrientation="behind" >
 * </activity>
 *
 *
 *
 */
public class PayActivity extends BaseActivity
{

    private WXManager wxManager;
    private AliManager aliManager;
    @BindView(R.id.lay)
    LinearLayout lay;

    private StackBlurManager m;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        wxManager = WXManager.getInstance(this);
        aliManager = AliManager.getInstance(this);
        m = new StackBlurManager();
        Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ewr);
        bmp = m.blur(bmp, 120);
        lay.setBackground(new BitmapDrawable(bmp));
    }

    @Override
    public int getContentView()
    {
        return R.layout.activity_pay;
    }

    @OnClick(value = {R.id.btn_wx_pay, R.id.btn_alipay, R.id.btn_unionpay})
    public void click(View v)
    {
        switch (v.getId())
        {
            case R.id.btn_wx_pay:
                Map<String,String> params = new HashMap<>();
                params.put("body", "圣诞气锤");
                params.put("total_fee", "1");
                wxManager.doPay(params, Constant.WEB_BASE + "wxpay/getPrepayId", new WXManager.WXPayCallback()
                {
                    @Override
                    public void onSatrt()
                    {
                        showProgress();
                    }

                    @Override
                    public void onFailed(String msg)
                    {
                        hideProgress();
                        Toast.makeText(PayActivity.this, msg, Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onSuccess(String msg)
                    {
                        hideProgress();
                        Toast.makeText(PayActivity.this, "因为没有合法的APPID，所以不能调起支付", Toast.LENGTH_SHORT).show();
                    }
                });
                break;

            case R.id.btn_alipay:
                Map<String, String> param = new HashMap<>();
                param.put("subject", "barrete");
                param.put("body", "weapon");
                param.put("price", "1");
                aliManager.doPayWithClient(Constant.WEB_BASE + "alipay/asd", param,new AliManager.AliPayResultCallBack()
                {
                    @Override
                    public void onComplete(String resultStatus, String resultDetailMsg)
                    {
                        hideProgress();
                        Toast.makeText(PayActivity.this, resultDetailMsg, Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onFailure(String simpleMsg, String detail)
                    {
                        hideProgress();
                        Toast.makeText(PayActivity.this, simpleMsg, Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onWait()
                    {
                        showProgress();
                    }
                });
                break;
            case R.id.btn_unionpay:
                new BaseDialog.Builder(this)
                        .setTitle("赤壁怀古")
                        .setMessage("大江东去，浪淘尽，千古风流人物。")
                        .setRation(8)
                        .setPositiveListener("OK", v1 -> {})
                        .setNegativeListener("CANCEL", v1 -> {})
                        .setBlurColor(Color.parseColor("#22AA8888"))
                        .show();
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);
        if (data == null)
        {
            return;
        }

        String str = data.getExtras().getString("pay_result");
        if (str.equalsIgnoreCase("success"))
        {
            // 支付成功后，extra中如果存在result_data，取出校验
            // result_data结构见c）result_data参数说明
            if (data.hasExtra("result_data"))
            {
                String sign = data.getExtras().getString("result_data");
                // 验签证书同后台验签证书
                // 此处的verify，商户需送去商户后台做验签
//                if (verify(sign))
//                {
//                    //验证通过后，显示支付结果
//                } else
//                {
            // 验证不通过后的处理
            // 建议通过商户后台查询支付结果
                }
            else
            {
            // 未收到签名信息
            // 建议通过商户后台查询支付结果
            }
        } else if (str.equalsIgnoreCase("fail"))
        {
            //            showResultDialog(" 支付失败！ ");
        } else if (str.equalsIgnoreCase("cancel"))
        {
            //            showResultDialog(" 你已取消了本次订单的支付！ ");
        }

    }
}
